package service

import (
	"database/sql"
	"htdemo.com/goim/logic/dao"
	"htdemo.com/goim/public/imctx"
	"htdemo.com/goim/public/imerror"
)

type authService struct{}

var AuthService = new(authService)

func (*authService) Auth(ctx *imctx.Context, deviceToken string) (userId, deviceId int64, err error) {
	device, e := dao.DeviceDao.GetByToken(ctx, deviceToken)
	if e == sql.ErrNoRows {
		return 0, 0, imerror.LErrDeviceIdOrToken
	}
	if e != nil {
		lError := imerror.LErrUnknow
		lError.Message = e.Error()
		return 0, 0, lError
	}

	if device.Id == 0 {
		return 0, 0, imerror.LErrDeviceIdOrToken
	}

	return device.UserId, device.Id, nil
}
